<?php

/**
 * style short summary.
 *
 * style description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Style extends Goods_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Goods_style_model');
        $this->load->library('form_validation');
    }

    public function stylelist()
    {
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;

        $pagesize=10;
        $one=$this->Goods_style_model->get_page($cupge,$pagesize,"*","goods_style","order by goods_style_id desc","goods_style_id","");
        $this->load->library('page');
        $p=new Page();
        $data['styleList']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/style/stylelist'));
        $data['links']=$p->showPages(1);
        $this->load->view('Admin/style/stylelist.html',$data);

    }

    public function addstyle()
    {
        $this->load->view('Admin/style/addstyle.html');
    }

    public function add_style()
    {
        $valiRes= $this->form_validation->run('style_vali');
        $this->load->library('ajaxMsgModel');
        $styleModel= new Goods_style_model();
        if($valiRes)
        {
            $styleModel->title=$this->input->post('title');

            $res= $styleModel->insert_goods_style($styleModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/style/stylelist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editstyle()
    {
        $styleId=$this->uri->segment(4);
    	$data['styleModel']=$this->Goods_style_model->get_goods_style_by_id($styleId);
        $this->load->view('Admin/style/editstyle.html',$data);
    }

    public function edit_style()
    {
        $valiRes= $this->form_validation->run('style_vali');
        $this->load->library('ajaxMsgModel');
        $styleModel= new Goods_style_model();
        if($valiRes)
        {
            $styleModel->goods_style_id=$this->input->post('goods_style_id');
            $styleModel->title=$this->input->post('title');
            
            $res= $styleModel->edit_goods_style($styleModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/style/stylelist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }



    public function del_style()
    {
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $res=$this->Goods_style_model->del_goods_style_byid($this->uri->segment(4));
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batchdel_style()
    {
        $goods_style_id=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $styleModel=new $this->Goods_style_model();
        $goods_style_idArray=explode('|',trim($goods_style_id,'|'));
        $res=0;
        if(count($goods_style_idArray)>0)
        {
            $res=$styleModel->batchdel_goods_style($goods_style_idArray);
        }
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}